package actionHandlers;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import cuShapes.PQShape;
import cuShapes.ShapeList;

import palette.CanvasManager;
import palette.CanvasWindow;
import palette.DrawingWindow;

public class ShapeTreeSelectListener implements MouseListener{

	@Override
	public void mouseClicked(MouseEvent arg0) {
		
		JTree tree = ((DrawingWindow) (CanvasManager.getInst().getSelectedFrame())).getShapeTree().getTree();
		TreePath selPath = tree.getPathForLocation(arg0.getX(), arg0.getY());
		
		// The user can click on the JTree, but not necessarily on a TreeNode.
		// This try block catches the times the user does not click on a TreeNode, and does nothing with it.
		
		
		try {
			DefaultMutableTreeNode tempNode = (DefaultMutableTreeNode) selPath.getLastPathComponent();
			ShapeList list = (ShapeList)((CanvasWindow)CanvasManager.getInst().getSelectedFrame()).getShapeList();
			list.setCurrentShape((PQShape)tempNode.getUserObject());
			list.update();
		} catch (Exception e) {
			//System.out.println("Error exists");
				//e.printStackTrace();
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		
	}

}
